package com.vivz.baby.http.client;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vivz.baby.domain.Log;
import com.vivz.baby.enumeration.LogEntryType;
import com.vivz.baby.service.http.IHttpLogService;

/**
 * Http Log Service Client
 *
 * @author Vivek More
 *
 */
public class HttpLogServiceClient {
	
	private static final Logger LOGGER = Logger.getLogger(HttpLogServiceClient.class);
	
	/**
	 * Main method of the Http Log Service Client
	 */
	public static void main(final String[] args) {
		
		HttpLogServiceClient.LOGGER.debug("Http Log Service Client is starting...");
		
		// Http Client Application Context is started...
		final ApplicationContext context = new ClassPathXmlApplicationContext("httpClientAppContext.xml");
		
		// Remote Log Service is called via Http Client Application Context...
		final IHttpLogService client = (IHttpLogService) context.getBean("HttpLogService");
		HttpLogServiceClient.find(client, null);
		
		HttpLogServiceClient.LOGGER.debug("Http Log Service Client is stopped...");
	}
	
	private static void log(final IHttpLogService client) {
		final Date today = new Date();
		final Log data = new Log();
		final long currentTimeMillis = System.currentTimeMillis();
		
		data.setLogTime(today);
		data.setLogEntryType(LogEntryType.BREAST_FEEDING_LEFT);
		data.setValue(String.valueOf((int) currentTimeMillis % 15 + 5));
		// data.setRightBreast((int) currentTimeMillis % 20 + 5);
		// data.setPumped((int) currentTimeMillis % 120 + 5);
		// data.setFormula((int) currentTimeMillis % 60 + 5);
		// data.setDiaper(currentTimeMillis % 2 == 0 ? "WD" : "DT");
		data.setUpdatedBy("HttpLogServiceClient");
		data.setUpdatedOn(today);
		
		client.log(data);
	}
	
	private static void find(final IHttpLogService client, final Long userId) {
		
		final List<Log> thisYearsLogs = client.getThisYearsLogs(userId);
		
		if (CollectionUtils.isNotEmpty(thisYearsLogs)) {
			for (final Log log : thisYearsLogs) {
				System.out.println(log);
			}
		}
	}
	
}
